/********************************************************************************
  Program: D20 Chat
  Authors: Frank Hindman, Adam Bauerle, Scott Bachmeier, and Mindy Bachmeier 
  Class: CSC 465 Senior Design
  Instructor: Dr. Wei
  Date: 5/8/2007 (the date of the final presentation)
  Description: 
  This program is designed to be a chatting interface for a game 
  master to hold a D20 based game over the Internet with several players. 
  It will serve as the paper, dice, and table the group would share if they 
  were meeting in person.
  Input & Output:
  This program will use text files to store system info, character sheets,
  and notes. All files are generated by it, so no outside files are necessary.
  Compilation instructions:
  Usage:
  The program will be available for download in a zip folder at
  http://sourceforge.net/projects/d20-chat
  To install d20-chat after download:
  1) Download Java 6 from http://java.sun.com/javase/downloads/index.jsp
    a) Most users will want Java Runtime Environment (JRE) 6u1 (fourth one down)
  2) Copy contents of zip file to wherever you want.
  3) Double click the file d20-chat.jar to run.
  Known bugs/missing features:
  Modifications:
  Date                Comment            
  ----    ------------------------------------------------
********************************************************************************/

package d20chat;



import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.UIManager.*;


/**
 * The main class for the program, controls flow of the entire system.
 * The main starting point of the program exists within this class. It handles the
 * initial flow of the program, and also stores many variables that are accessed
 * by other classes.
 */
public class D20Chat 
{
    private Client clientthread;
    /**
     * Stores the current instance of Server (if running).
     */
    Server theserver;
    /**
     * Stores the current instance of the IncomingMessageParser.
     */
    IncomingMessageParser InParse;
    /**
     * Stores the current instance of the OutgoingMessageParser.
     */
    OutgoingMessageParser OutParse;
    /**
     * Stores the User that is associated with the current human running the program.
     */
    User theUser;
    /**
     * Stores the current DisplaySettings.
     */
    DisplaySettings theSettings;
    /**
     * Stores the address of the server to be connecting to.
     */
    String serverIP;
    /**
     * Stores the password of the server to be connecting to.
     */
    String serverPassword;
    /**
     * Stores the port of the server to be connecting to.
     */
    Integer serverPort;
    /**
     * Stores the current instance of d20chatui.
     */
    d20chatui gui;
    /**
     * Toggle to say if charsheets are enabled.
     */
    boolean charsheet;
    /**
     * Toggle to specify if the current instance is running as a server.
     */
    boolean isServer = false;
    /**
     * Toggle to specify if the current client is connected to a server.
     */
    boolean connected = false;
    private String defaultProfile;
    /**
     * Toggle to specify if the default profile should be loaded automatically.
     */
    boolean loadDefaultProfile = false;

/********************************************************************************
  Method:
  Description:
  Parameters:
  Used by:
  Uses:
********************************************************************************/
    /**
     * Class constructor for D20Chat.
     * Sets up the look and feel for the program, begins all of the required classes
     * running, and lets the program run it's course.
     */
    public D20Chat() 
    {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        theUser = new User();
        theUser.setUsername("noname");
        
        gui = new d20chatui(this);
        gui.setVisible(true);
       
        theSettings = new DisplaySettings(gui);
        
         
        //for now passing it gui so that can modify stuff directly
        //create the client thread, pass it gui so that it can post directly for now
        clientthread = new Client(gui, this);
        //give the gui the clientthread so that it can send messages directly
        //gui.tempMain = this;
        gui.tempclient = clientthread;
        
        
        OutParse = new OutgoingMessageParser(gui, clientthread, this );
        InParse = new IncomingMessageParser(gui, this );
        clientthread.InParse = InParse;
        clientthread.OutParse = OutParse;
        gui.OutParse = OutParse;
        
        ProfileManager profileManager;
        profileManager = new ProfileManager();
        profileManager.tempMain = this;
        //profileManager.tempGui = gui;
        profileManager.loadDefaultProfile();
    }
     
    
/**
     * Starts the program running a server, and connects the client to itself.
     * Begins the server running and listening for new connections. Connects the
     * program to itself (thus being the first, and being set as GM). Also sets the
     * charactername to "GM".
     */
    public void BeginServer()
    {
        if(isServer == false)
        {
            isServer = true;
            theserver = new Server(serverPort, gui, this, serverPassword );
            //theserver.tempmain = this;
            //gui.tempserver = theserver;
            clientthread.SetupConnection("localhost", serverPort);
            theUser.setCharacterName( "GM" );
        }
        else
        {
            gui.tempchatwindowoutput("Server already started, please shutdown server first");
        }
    }
    
    
/**
     * Connects the program to a server.
     * Uses the server and port settings that are already set to connect to a server.
     */
    public void OpenConnection()
    {
        clientthread.SetupConnection(serverIP, serverPort);
    }
    
    
/**
     * Begining point of the entire program.
     * Starts and instance of D20Chat and let's it's constructor begin the
     * initialization of the program.
     * @param args Command line arguements, currently not used.
     */
    public static void main(String[] args) 
    {
        //instanciate the d20chat class, and let the program start running
        D20Chat prog = new D20Chat ();
    }
    
    
}

